package produce

import (
	"data-process/config"
	"database/sql"
)

const (
	DriverMysql      = "mysql"
	DriverOracle     = "oracle"
	DriverPostgresql = "postgresql"
)

type ItemDataMap map[string]interface{}

// DataSource 数据源相关接口
type DataSource interface {
	buildSource() (*sql.Conn, error)                       // 构建数据源
	readData(data chan<- map[string]interface{}, size int) // 读取数据
}

type CommonInfo struct {
	database string
	port     int
	ip       string
	username string
	password string
}

type DbProduce struct {
	Produce
	source DataSource
}

func DefaultDbProduce() *DbProduce {

	return NewDbProduce()
}

func NewDbProduce() *DbProduce {
	p := config.SysConfig.Produce
	d := new(DbProduce)
	switch p.Database.Driver {
	case DriverMysql:
		d.source = &MysqlDbProduce{
			sql: p.Database.Sql,
			CommonInfo: CommonInfo{
				ip:       p.Database.Ip,
				port:     p.Database.Port,
				username: p.Database.Username,
				password: p.Database.Password,
				database: p.Database.Db,
			},
		}
	case DriverOracle:
	case DriverPostgresql:

	}
	d.Produce = Produce{
		Type:     p.Type,
		Size:     config.SysConfig.Size,
		DataPool: make(chan map[string]interface{}, 10),
		producer: d.producer,
	}
	return d
}

func (d *DbProduce) producer() {
	d.source.readData(d.DataPool, d.Size)
}
